/*
 * Copyright (C) 2008 Chris Bellew
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "IntervalPickerView.h";
#import <UIKit/UITable.h>
#import <UIKit/UIView.h>
#import <UIKit/UIImageAndTextTableCell.h>
#import <UIKit/UIPreferencesTableCell.h>

#import "iDynDNSController.h";
#import "iDynDNSSettings.h";
#import "iDynDNSSettingsView.h";

@class UITable;

static IntervalPickerView *_sharedInstance = nil;
#define UI_TOP_NAVIGATION_BAR_HEIGHT  45.0f
#define UI_BOTTOM_NAVIGATION_BAR_HEIGHT 40.0f

@implementation IntervalPickerView

+ (IntervalPickerView*)sharedInstance {
	if(!_sharedInstance) {
		_sharedInstance = [[IntervalPickerView alloc] init];
	}
	return _sharedInstance;
}

-(id)init {
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	_updateIntervalArray = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:15], [NSNumber numberWithInt:30], nil];
	return [self initWithFrame:rect];
}

// designated initializer
-(id)initWithFrame:(CGRect) rect {
	self = [super initWithFrame: rect];
 
	_sharedInstance = self;

	_navigationBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, rect.size.width, NAVBAR_HEIGHT)];
	[_navigationBar showButtonsWithLeftTitle:@"Back" rightTitle:nil leftBack:YES];
    [_navigationBar setDelegate:self];
    [_navigationBar enableAnimation];
   	UINavigationItem *navTitle;
   	navTitle = [[UINavigationItem alloc] initWithTitle: @"iDynDNS"];
   	[_navigationBar pushNavigationItem:navTitle];
    [_navigationBar setButton:0 enabled:false]; 
   
	_table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0.0, NAVBAR_HEIGHT, rect.size.width, rect.size.height - NAVBAR_HEIGHT)];
	
	[_table setDelegate:self];
	[_table setDataSource:self];
	
	
	[self addSubview:_navigationBar];
	[self addSubview:_table];
	
	[_table reloadData];
	
	return self;
}

- (void) dealloc {
	[_table release];
	[_navigationBar release];
	[_updateIntervalArray release];
	[super dealloc];
}


- (int) numberOfGroupsInPreferencesTable: (UIPreferencesTable*)table
{
	return 1;
}

- (int) preferencesTable: (UIPreferencesTable*) table numberOfRowsInGroup: (int) group
{
	return 3; //change later
}

- (UIPreferencesTableCell*)preferencesTable:(UIPreferencesTable*)table cellForGroup: (int)group {
	return [[UIPreferencesTableCell alloc] init];
}

- (UIPreferencesTableCell*) preferencesTable: (UIPreferencesTable*)table cellForRow: (int)row inGroup: (int)group {
	
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	[cell setTitle:[[NSString alloc] initWithFormat:@"%@ minutes.", [_updateIntervalArray objectAtIndex:row]]];
	[cell setTapDelegate:self];
	if([[[iDynDNSSettings sharedInstance] updateInterval] intValue] == [[_updateIntervalArray objectAtIndex:row] intValue]) {
		[cell setChecked:YES];
	}
	return cell;
}

- (void)tableRowSelected:(NSNotification *)notification {
	int row = [_table selectedRow];
	
	[[iDynDNSSettings sharedInstance] setUpdateInterval:[_updateIntervalArray objectAtIndex:row - 1]];
	[[iDynDNSSettings sharedInstance] saveSettings];
	[_table reloadData];
	[[_table cellAtRow:row column:0] setSelected:NO withFade:YES];
	[[[iDynDNSSettingsView sharedInstance] prefTable] reloadData]; // Interval has changed, so update the main screen so it reflects the new value
}

- (BOOL)preferencesTable:(UIPreferencesTable *)table isRadioGroup:(int)group
{
    return YES;
}

-(void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button{
	if (button == 1){
		[[iDynDNSController sharedInstance] showView:[iDynDNSSettingsView sharedInstance] event:2];
	}
}

@end

